<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>生成一个伪随机字节串</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.openssl-random-pseudo-bytes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_random_pseudo_bytes</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7)</p><p class="refpurpose"><span class="refname">openssl_random_pseudo_bytes</span> &mdash; <span class="dc-title">生成一个伪随机字节串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-random-pseudo-bytes-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>openssl_random_pseudo_bytes</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter reference">&$crypto_strong</code></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   生成一个伪随机字节串 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> ，字节数由 <code class="parameter">length</code> 参数指定。 
  </p>
  <p class="para">
   通过 <code class="parameter">crypto_strong</code> 参数可以表示在生成随机字节的过程中是否使用了强加密算法。返回值为<strong><code>FALSE</code></strong>的情况很少见，但已损坏或老化的有些系统上会出现。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-random-pseudo-bytes-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       所需字节串的长度，必须为正整数。PHP会试着将该参数转换为非空整数来使用它。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">crypto_strong</code></dt>

     <dd>

      <p class="para">
       如果传递到该函数中，将会保存为一个 <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> 值来表明是否使用了“强加密”，如果被用于GPG和密码之类的将返回<strong><code>TRUE</code></strong> , 否则返回 <strong><code>FALSE</code></strong>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-random-pseudo-bytes-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功，返回生成的字节串 <span class="type"><a href="language.types.string.html" class="type string">string</a></span> ，  或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.openssl-random-pseudo-bytes-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-943">
    <p><strong>Example #1 <span class="function"><strong>openssl_random_pseudo_bytes()</strong></span> 范例：</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$hex&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Lengths:&nbsp;Bytes:&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">&nbsp;and&nbsp;Hex:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Lengths: Bytes: -1 and Hex: 0
string(0) &quot;&quot;
NULL

Lengths: Bytes: 0 and Hex: 0
string(0) &quot;&quot;
NULL

Lengths: Bytes: 1 and Hex: 2
string(2) &quot;42&quot;
bool(true)

Lengths: Bytes: 2 and Hex: 4
string(4) &quot;dc6e&quot;
bool(true)

Lengths: Bytes: 3 and Hex: 6
string(6) &quot;288591&quot;
bool(true)

Lengths: Bytes: 4 and Hex: 8
string(8) &quot;ab86d144&quot;
bool(true)

</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-random-pseudo-bytes-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="random_bytes.html" class="function" rel="rdfs-seeAlso">random_bytes()</a> - Generates cryptographically secure pseudo-random bytes</span></li>
   <li class="member"><span class="function"><a href="bin2hex.html" class="function" rel="rdfs-seeAlso">bin2hex()</a> - 函数把包含数据的二进制字符串转换为十六进制值</span></li>
   <li class="member"><span class="function"><a href="crypt.html" class="function" rel="rdfs-seeAlso">crypt()</a> - 单向字符串散列</span></li>
   <li class="member"><span class="function"><a href="mt_rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a> - 生成更好的随机数</span></li>
   <li class="member"><span class="function"><a href="uniqid.html" class="function" rel="rdfs-seeAlso">uniqid()</a> - 生成一个唯一ID</span></li>
  </ul>
 </div>

</div></div></div></body></html>